﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Dimok.Web.UI.JS.MSAjaxExtentions.js" assembly="Dimok.Web.UI"/>
/// <reference name="Dimok.Web.UI.JS.JSON_Lib.js" type="Dimok.Web.UI"/>
Type.registerNamespace("Dimok");

Dimok.MainMenu = function() {
  Dimok.MainMenu.initializeBase(this);
  this._initProps({ _mainMenu: null, _mainWindowName: '' });
  this.showLoanPageHandler = Function.createDelegate(this, this.showLoanPage);
  this.showingLoanPagesMenuHandler = Function.createDelegate(this, this.showingLoanPagesMenu);
}

Dimok.MainMenu.prototype = {
  openWindow: function(url, windowName, parameters) {
    windowName = windowName.replace(/[^A-Z0-9]/ig, "_");
    url = url.replace("#", "");
    var w = this.get_menuOpenWindows(windowName);
    if (!w && url.toLowerCase() == this._top().location.pathname.toLowerCase()) w = this._top();
    var openNew = true;
    try { openNew = !w || w.closed || !w.document; } catch (e) { }
    if (openNew)
      w = this.get_menuOpenWindows()[windowName] =
        open(url, windowName, parameters || "resizable,status=no,toolbar=no,titlebar=no,location=no,menubar=no,scrollbars=yes");
    setTimeout(function() { try { w.focus(); } catch (e) { } }, 100);
  },
  get_menuOpenWindows: function(name) { return name ? this.get_mainMenu().menuOpenWindows[name] : this.get_mainMenu().menuOpenWindows },
  set_menuOpenWindows: function(v) { this.get_mainMenu().menuOpenWindows = v },
  showLoanPage: function(sender, eventArgs) {
    if (!eventArgs.get_targetElement()) return alert("Sorry! Try again.");
    var ID = _first(eventArgs.get_targetElement().keys);
    switch (eventArgs.get_item().get_value()) {
      case "LoanInfo":
        return Dimok.FindMainMenu().openWindow("/Manager/Underwriting/FQuest/Decision.aspx?ApplID=" + ID + "&NoLock=1", "CREDIT_" + ID);
      case "ShowImages":
        return Dimok.FindMainMenu().openWindow("/Manager/Imaging/Imaging.aspx?ApplID=" + ID + "&", "IMAGES_" + ID);
      case "ShowCredit":
        return Dimok.FindMainMenu().openWindow("/Manager/Common/Credit.aspx?ApplID=" + ID, "CREDIT_" + ID);
      case "ShowDecision":
        return showModelessDialog("/manager/GuideLines/GLRules.aspx?BankID=22&ProgID=208&ApplID=" + ID + "&HideMenu=1", null, "status=0;resizable=1");
    }
    function _first(o) {
      for (var n in o)
        return o[n];
    }
  },
  showLoanPagesMenu: function(e) {
    cmLoanPages().show(e);
  },
  showingLoanPagesMenu: function(s, e) {
    debugger;
  },
  initialize: function() {
    Dimok.MainMenu.callBaseMethod(this, 'initialize');
    Sys.Application.add_load(Function.createDelegate(this, function() {
      cmLoanPages().add_itemClicked(this.showLoanPageHandler);
      //cmLoanPages().add_showing(this.showingLoanPagesMenuHandler);
    }));
    if (!this._top()._mainMenu) {
      this.set_mainMenu(this._top()._mainMenu = {});
      this.set_menuOpenWindows({});
      this.get_menuOpenWindows()[this.get_mainWindowName()] = this._top();
    } else
      this.set_mainMenu(this._top()._mainMenu);
  },
  _top: function() {
    try {
      var w = top;
      while (w.location.href != w.top.location.href) w = w.top;
      if (w.opener) w = w.opener;
      if (w.dialogArguments && w.dialogArguments.opener) w = w.dialogArguments.opener;
      while (w.location.href != w.top.location.href) w = w.top;
      return w;
    } catch (e) {
      status = "Events:" + e.description;
      return top;
    }
  },
  dispose: function() {
    if (this._top() == self)
      for (var wn in this.get_menuOpenWindows())
      try { this.get_menuOpenWindows(wn).close() } catch (e) { }
    cmLoanPages().remove_itemClicked(this.showLoanPageHandler);
    cmLoanPages().remove_itemClicked(this.showingLoanPagesMenuHandler);
    Dimok.MainMenu.callBaseMethod(this, 'dispose');
  }
}
Dimok.MainMenu.registerClass('Dimok.MainMenu', motobankcorp.Component);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
